﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cms.Model;
using System.Xml;
namespace Cms.BLL
{
    public class RssBuilder
    {
        public static Model.RssBody GetRssItem(RssSource source, bool encryptLink)
        {
            Model.RssBody Result=GetRssItem(source);
            if (encryptLink)
            {
                foreach (RssItem rssItem in Result.ItemList)
                {
                    rssItem.Link = String.Format("/tools/aspx.ashx?catelog=skip&key={0}&encode=1", EntLib.Utiltiy.MD5Encrypt(rssItem.Link));
                }
            }
            return Result;
        }
        /// <summary>
        /// 获取RSS 内容
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Model.RssBody GetRssItem(RssSource source)
        {
            string RssContent = "";
            if (source.SourceType == RssSourceType.Url)
                RssContent = EntLib.Utiltiy.GetHttpData(source.Source);
            else if (source.SourceType == RssSourceType.Content)
                RssContent = source.Source;
            if (String.IsNullOrEmpty(RssContent))
                return null;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(RssContent);
            RssChannel rssChannel = new RssChannel();
            //每个RSS只有一个频道节点
            XmlNode xnChanel = xmlDoc.SelectSingleNode("rss/channel");
            if (xnChanel == null)
                return null;
            rssChannel.Title = EntLib.Utiltiy.GetXmlChildText(xnChanel, "title");
            rssChannel.Description = EntLib.Utiltiy.GetXmlChildText(xnChanel, "description");
            rssChannel.Link = EntLib.Utiltiy.GetXmlChildText(xnChanel, "link");
            string strPubDate = EntLib.Utiltiy.GetXmlChildText(xnChanel, "pubDate");
            DateTime dtMin = DateTime.MinValue;
            if (!String.IsNullOrEmpty(strPubDate) && DateTime.TryParse(strPubDate, out dtMin))
                rssChannel.pubDate = dtMin;
            strPubDate = EntLib.Utiltiy.GetXmlChildText(xnChanel, "lastBuildDate");
            if (!String.IsNullOrEmpty(strPubDate) && DateTime.TryParse(strPubDate, out dtMin))
                rssChannel.lastBuildDate = dtMin;
            if (source.SourceType == RssSourceType.Url)
                rssChannel.Url = source.Source;
            Model.RssBody rssBody = new RssBody();
            rssBody.Chanel = rssChannel;
            foreach (XmlNode xnItem in xnChanel.SelectNodes("//item"))
            {
                RssItem rssItem = new RssItem();
                if (xnItem.SelectNodes("image").Count > 0)
                {
                    rssItem.Image = new RssItemImage();
                    rssItem.Image.Title = EntLib.Utiltiy.GetXmlChildText(xnItem, "image/title");
                    rssItem.Image.Description = EntLib.Utiltiy.GetXmlChildText(xnItem, "image/description");
                    rssItem.Image.Link = EntLib.Utiltiy.GetXmlChildText(xnItem, "image/link");
                }
                rssItem.Description = EntLib.Utiltiy.GetXmlChildText(xnItem, "description");
                strPubDate = EntLib.Utiltiy.GetXmlChildText(xnItem, "pubDate");
                if (!String.IsNullOrEmpty(strPubDate) && DateTime.TryParse(strPubDate, out dtMin))
                {
                    rssItem.pubDate = dtMin;
                }
                rssItem.Title = EntLib.Utiltiy.GetXmlChildText(xnItem, "title");
                rssItem.Link = EntLib.Utiltiy.GetXmlChildText(xnItem, "link");                
                rssBody.ItemList.Add(rssItem);
            }
            return rssBody;
        }
    }
}
